#include "logger.h"
#include "spdlog/async.h"
#include "spdlog/async_logger.h"
#include "spdlog/sinks/rotating_file_sink.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include <vector>

namespace zh
{
    Logger::Logger()
    {
        spdlog::init_thread_pool(8192, 1);

        auto stdout_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
        stdout_sink->set_level(spdlog::level::trace);
        stdout_sink->set_pattern("[%^%l%$] %v");

        auto rotating_sink =
            std::make_shared<spdlog::sinks::rotating_file_sink_mt>("logs/log.txt", 1024 * 1024 * 10, 3);
        std::vector<spdlog::sink_ptr> sinks {stdout_sink, rotating_sink};

        m_logger = std::make_shared<spdlog::async_logger>(
            "zh::logger", sinks.begin(), sinks.end(), spdlog::thread_pool(), spdlog::async_overflow_policy::block);
        m_logger->set_level(spdlog::level::debug);
        spdlog::register_logger(m_logger);
    }

    Logger::~Logger()
    {
        m_logger->flush();
        spdlog::drop_all();
    }

    Logger& Logger::getInstance()
    {
        static Logger s_instance;
        return s_instance;
    }
} // namespace zh